package talk.Utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.widget.Toast;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by debby on 2018/4/9.
 * 描述:微信分享
 */

public class OpenWeChat {

    //com.tencent.mm.ui.tools.ShareImgUi   是分享到微信好友

    //com.tencent.mm.ui.tools.ShareToTimeLineUI  是分享到微信朋友圈

    //intent.put("kdescription","分享朋友圈的图片说明");
    //微信好友
    public static String ShareImgUi = "com.tencent.mm.ui.tools.ShareImgUi";
    //微信朋友圈
    public static String ShareToTimeLineUI = "com.tencent.mm.ui.tools.ShareToTimeLineUI";

    /**
     * 判断是否安装微信
     *
     * @param type     分享类型
     * @param filePath 分享文件地址
     */
    public static void share(Context context, String type, String filePath) {
        if (isWeixinAvilible(context)) {
            Toast.makeText(context, "您还没有安装微信", Toast.LENGTH_SHORT).show();
        } else {
            initShareVideoIntent(context, type, filePath);
        }

    }


    private static void initShareVideoIntent(Context context, String type, String filePath) {
        boolean found = false;
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("video/*");
//        share.setType("image/*");
        share.setType("text/plain");

        List<ResolveInfo> resInfo = context.getPackageManager().
                queryIntentActivities(share, PackageManager.MATCH_DEFAULT_ONLY);
        if (!resInfo.isEmpty()) {
            for (ResolveInfo info : resInfo) {
//                if (info.activityInfo.packageName.toLowerCase().contains(type) ||
//                        info.activityInfo.name.toLowerCase().contains(type) ) {
                // 这里是 微信分享的那个界面class 的全部路径
                if (info.activityInfo.name.contains(type)) {
//                    share.putExtra(Intent.EXTRA_SUBJECT,  "weixin share video");
//                    share.putExtra(Intent.EXTRA_TEXT,     "weixin share video...");
                    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath)));
                    share.setPackage(info.activityInfo.packageName);
                    // 微信和微信收藏是 packageName一样的,指定打开的class，就不会出现让用户选择的界面了
                    share.setClassName(info.activityInfo.packageName, info.activityInfo.name);
                    found = true;
                    break;
                }
            }
            if (!found)
                return;
//            startActivity(Intent.createChooser(share, "Select "));
            context.startActivity(share);
        }
    }


    /**
     * 判断是否安装了微信
     *
     * @return 是否安装了微信
     */
    private static boolean isWeixinAvilible(Context context) {
        // 获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        // 获取所有已安装程序的包信息
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }
        return false;
    }


    /**
     * 发邮件
     *
     * @param uri 文件地址
     */
    public static void sendMail(Context context, Uri uri) {
        if (uri == null) {
            return;
        }
        String[] tos = {" "};
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("message/rfc822"); // 设置邮件格式
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_EMAIL, tos);
        intent.putExtra(Intent.EXTRA_SUBJECT, "截图_"
                + getDate2String(System.currentTimeMillis())); // 主题
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        context.startActivity(Intent.createChooser(intent, "请选择邮件类应用"));
    }

    /**
     * 时间转换时间格式(毫秒级别)
     */
    private static String getDate2String(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(time);
    }

}



